import { ref, onMounted } from "vue";
import { Apis } from "@/apis";
import { ElMessage } from "element-plus";
interface paramsType {
	statu ?: number;
	content? : string;
	title ?: string;
	username ?: string;
  type?: string|number;
}
export const usePaging = (
	GetDataFun : Function,
	params : paramsType,
	pagesize : number = 10
) => {
	const pages = ref<number>(1);
	const pageSize = ref<number>(pagesize);
	const total = ref<number>(0);
	const data = ref<any[]>([]);
	const detailData = ref<any>({});
	const getData = async () : Promise<void> => {
		const res = await GetDataFun({
			pageNum: pages.value,
			pageSize: pageSize.value,
			...params,
		});
		if (res.data) {
			data.value = [];
			data.value = res.data;
			total.value = res.total;
		}
	};
	const refreshData = () : void => {
		data.value = [];
		pages.value = 1;
		getData();
	};
	const nextPage = () : void => {
		pages.value++;
		getData();
	};
	const prevPage = () : void => {
		pages.value--;
		getData();
	};
	const changePage = (page : number) : void => {
		pages.value = page;
		getData();
	};
	const filterHandler = (value : string, row : any, column : any) : boolean => {
		console.log(value, row, column);
		const property = column["property"];
		return row[property] === value;
	};
	const getPostDetail = async (id : number) : Promise<any> => {
		const res = await Apis.getPostDetail({ id: id });
		if (res.data) {
			detailData.value = res.data;
			return Promise.resolve(); //等待获取到数据再出弹窗
		}
	};
	const passPost = async (id : number) : Promise<void> => {
		console.log(id);
		const res = await Apis.updatePostStatus({ id, statu: 1 });
		if (res.status == 200) {
			getData();
			ElMessage.success("审核通过");
		}
	};
  const deletePost = async (id : number) : Promise<void> => {
		const res = await Apis.deletePostItem({ id });
		if (res.status == 200) {
			getData();
			ElMessage.success("删除成功");
		}
  }
	onMounted(() => {
		getData();
	});
	return {
		pages,
		total,
		data,
		pagesize,
		detailData,
		getData,
		refreshData,
		filterHandler,
		nextPage,
		prevPage,
		changePage,
		getPostDetail,
		passPost,
    deletePost
	};
};
